//
//  UIBarButtonItem+Additional.m
//  RaiyiCommon
//
//  Created by zhiwei ma on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIBarButtonItem+Additional.h"

@implementation UIBarButtonItem (Additional)
- (id)initWithCustom:(UIImage*)aImage bgImage:(UIImage*)aBgImage
              target:(id)aTarget action:(SEL)aAction
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    if (aBgImage)
    {
        button.frame = CGRectMake(0, 0, aBgImage.size.width, aBgImage.size.height);
    }
    else
    {
        button.frame = CGRectMake(0, 0, aImage.size.width + 5, aImage.size.height);
    }
    
    [button setImage:aImage forState:UIControlStateNormal];
    
    if (aBgImage)
    {
        [button setBackgroundImage:aBgImage forState:UIControlStateNormal];
    }
    
    [button addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside];
    
    self = [self initWithCustomView:button];
    self.target = aTarget;
    self.action = aAction;
    return self;
}

- (id)initWithCustomWithTitle:(NSString*)aTitle bgImage:(UIImage*)aBgImage
                       target:(id)aTarget action:(SEL)aAction
{
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIFont* font = [UIFont systemFontOfSize:13.0f];
    button.titleLabel.font = font;
    CGSize size = [aTitle sizeWithFont:font];
    NSInteger buttonWidth = 0;
    if (size.width < 41)
    {
        buttonWidth = 41;
    }
    else 
    {
        buttonWidth = 60;
    }
    
    button.frame = CGRectMake(0,0,buttonWidth,29);
    
    [button setTitle:aTitle forState:UIControlStateNormal];
    [button setBackgroundImage:aBgImage forState:UIControlStateNormal];
    [button addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside];
    
    self = [self initWithCustomView:button];
    self.target = aTarget;
    self.action = aAction;
    return self;
}

- (id)initWithCustomWithTitle:(NSString*)aTitle bgImage:(UIImage*)aBgImage
                       target:(id)aTarget action:(SEL)aAction useTitleWidth:(BOOL)useTitleWidth
{
    if (useTitleWidth)
    {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        UIFont* font = [UIFont systemFontOfSize:13.0f];
        button.titleLabel.font = font;
        CGSize size = [aTitle sizeWithFont:font];
        
        button.frame = CGRectMake(0, 0, size.width + 20, 29);
        
        [button setTitle:aTitle forState:UIControlStateNormal];
        [button setBackgroundImage:[aBgImage stretchableImageWithLeftCapWidth:5 topCapHeight:2] forState:UIControlStateNormal];
        [button addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside];
        
        self = [self initWithCustomView:button];
        self.target = aTarget;
        self.action = aAction;
        return self;
    }
    else
    {
        return [self initWithCustomWithTitle:aTitle bgImage:aBgImage target:aTarget action:aAction];
    }
}

- (id)initWithCustomWithTitle:(NSString*)aTitle bgImage:(UIImage*)aBgImage
                       target:(id)aTarget action:(SEL)aAction useBgImageWidth:(BOOL)useBgImageWidth
{
    if (useBgImageWidth)
    {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        UIFont* font = [UIFont systemFontOfSize:13.0f];
        button.titleLabel.font = font;
        
        button.frame = CGRectMake(0,0,aBgImage.size.width,aBgImage.size.height);
        
        [button setTitle:aTitle forState:UIControlStateNormal];
        [button setBackgroundImage:aBgImage forState:UIControlStateNormal];
        [button addTarget:aTarget action:aAction forControlEvents:UIControlEventTouchUpInside];
        
        self = [self initWithCustomView:button];
        self.target = aTarget;
        self.action = aAction;
        return self;
    }
    else
    {
        return [self initWithCustomWithTitle:aTitle bgImage:aBgImage target:aTarget action:aAction];
    }
}
@end
